<div class="order-box">
    <ol class="flow-steps">
        <li class="current_a"><span class="first">1.Select Product</span></li>
        <li class="current"><span>2.Comfirm Information</span></li>
        <li class=""><span>3.Check Out</span></li>
        <li class="last"><span>4.Payment Processing</span></li>
        <div class="clear"></div>
    </ol>

    <br/>
    <table width="100%" class="order-list" cellspacing="0" cellpadding="4">
        <thead>
            <tr>
                <th align="left" style="padding-left: 10px;">Product</th>
                <th width="100">Price</th>
                <th width="100">Quantity</th>
                <th></th>
            </tr>
        </thead>
        <tbody>
            <?php
            $total_price = 0;
            if (!empty($cart->orders)):
                foreach ($cart->orders as $key => $order):
                    $product = Orders::model()->getProduct($order, $order['num']); //获取产品名称和价格
                    $currency_price = WebCurrency::model()->changeCurrency($product['price']); //转为相应汇率价
                    $total_price += $currency_price['price']; //计算总价
                    $game_name = ProductCategory::model()->getGameNameById($order['game_id']);
                    $server = ProductCategory::model()->findByPk($order['server_id']);
                    ?>
                    <tr>
                        <td>
                            <b><?php echo $product['product_name']; ?></b>
                            <ul>
                                <li><i><?php echo "<b>Game:</b> {$game_name} <b>Server:</b> {$server->title}"; ?></i></li>
                                <li>
                                    <i>
                                        <?php
                                        if ($order['input']) {
                                            foreach ($order['input'] as $attr_key => $attr_value) {
                                                echo "<b>$attr_key</b>: $attr_value ";
                                            }
                                        }
                                        ?>
                                    </i>
                                </li>
                            </ul>
                        </td>
                        <td align="center"><?php echo $currency_price['tag'] . $currency_price['price']; ?></td>
                        <td align="center" class="order-num">
                            <?php
                            switch ($order['product_type']) {
                                case 'GOLD'://金币
                                case 'CARD'://点卡
                                    echo CHtml::link('- ', 'javascript:void(0)', array('onclick' => 'reduce(1, this)', 'rel-key' => $key));
                                    echo CHtml::textField("num[$key]", $order['num'], array('readonly' => true, 'class' => 'span1'));
                                    echo CHtml::link(' +', 'javascript:void(0)', array('onclick' => 'increase(1, this)', 'rel-key' => $key));
                                    break;
                                default :
                                    echo $order['num'];
                            }
                            ?>
                        </td>
                        <td align="center" width="40"><?php echo CHtml::link('Delete', Yii::app()->createAbsoluteUrl('order/order/remove', array('key' => $key)), array('class' => 'remove', 'title' => 'Remove')); ?></td>
                    </tr>
                    <?php
                endforeach;
            endif;
            ?>
        </tbody>
        <tfoot>
            <tr>
                <td colspan="4" align="center">
                    <?php
                    if (!empty($cart->orders)) {
                        ?>
                        <B>Total: <span id="total-price" class="total-price"><?php echo $currency_price['tag'] . $total_price; ?></span></B>
                        <?php
                    } else {
                        echo 'Empty';
                    }
                    ?>
                </td>
            </tr>
        </tfoot>
    </table>

    <div class="order-informaction">
        <h3>Information</h3>
        <?php 
        if (Yii::app()->user->isGuest):
        ?>
        <div style="position: absolute;margin-left: 500px;border-left: solid 1px #ddd;padding-left: 20px;height:200px;">
            <?php
                $this->renderPartial('//site/login', array('model' => new LoginForm, 'action' => 'cart'));
            ?>
        </div>
        <?php 
        endif;
        ?>
        <form method="post" action="<?php echo Yii::app()->createAbsoluteUrl('order/order/addInformaction'); ?>" onsubmit="return checkForm()">
            <ul class="shopping-info" style="width: 600px;">
                <li>
                    <label for="coupon">Coupon: </label>
                    <div class="input-append">
                        <input type="text" value="" name="Customer[coupon]" id="coupon">&nbsp;
                        <button type="button" class="btn" onclick="checkCoupon()">Check</button>
                    </div>
                    <div class="clear"></div>
                </li>
                <li>
                    <label for="f_name">First name <font color="red">*</font>: </label>                        <div class="input-box">
                        <input type="text" class="flowfVerify" rel-verify="required" rel-message="First name must be enter." value="<?php echo isset($customer['f_name']) ? $customer['f_name'] : ''; ?>" name="Customer[f_name]" id="f_name" />

                    </div>
                    <div class="clear"></div>
                </li>
                <li>
                    <label for="l_name">Last name <font color="red">*</font>: </label>                        <div class="input-box">
                        <input type="text" class="flowfVerify" rel-verify="required" rel-message="Last name must be enter." value="<?php echo isset($customer['l_name']) ? $customer['l_name'] : ''; ?>" name="Customer[l_name]" id="l_name" />
                    </div>
                    <div class="clear"></div>
                </li>
                <li>
                    <label for="email">Email Address <font color="red">*</font>: </label>                        <div class="input-box">
                        <input size="50" type="text" value="<?php echo isset($customer['email']) ? $customer['email'] : ''; ?>" class="flowfVerify" rel-verify="required" rel-message="Email Address must be enter." name="Customer[email]" id="email" />
                    </div>
                    <div class="clear"></div>
                </li>
                <li>
                    <label for="country">Country: </label>
                    <div>
                        <?php
                        echo CHtml::dropDownList('Customer[country]', $customer['country'], array('' => 'Select you country') + CHtml::listData(UsCity::model()->findAll('parentid=0'), 'name', 'name'));
                        ?>
                    </div>
                    <div class="clear"></div>
                </li>
                <li>
                    <label for="city">City <font color="red">*</font>: </label>                        <div class="input-box">
                        <input type="text" class="flowfVerify" rel-verify="required" rel-message="City must be enter." name="Customer[city]" value="<?php echo isset($customer['city']) ? $customer['city'] : ''; ?>" id="city" />
                    </div>
                    <div class="clear"></div>
                </li>
                <li>
                    <label for="address">Address <font color="red">*</font>: </label>                        <div class="input-box">
                        <input size="50" type="text" class="flowfVerify" rel-verify="required" rel-message="Address must be enter." name="Customer[address]" id="address" value="<?php echo isset($customer['address']) ? $customer['address'] : ''; ?>" />
                    </div>
                    <div class="clear"></div>
                </li>
                <li>
                    <label for="tel">Contact Phone <font color="red">*</font>: </label>                        <div class="input-box">
                        <input type="text" class="flowfVerify" rel-verify="required" rel-message="Contact Phone must be enter." name="Customer[tel]" id="tel" value="<?php echo isset($customer['tel']) ? $customer['tel'] : ''; ?>" />
                    </div>
                    <div class="clear"></div>
                </li>
                <li>
                    <label for="msn">MSN: </label>
                    <div class="input-box"><input size="50" type="text"  name="Customer[msn]" id="msn" value="<?php echo isset($customer['msn']) ? $customer['msn'] : ''; ?>" /></div>
                    <div class="clear"></div>
                </li>
                <li>
                    <label for="comment">Comments: </label>
                    <div>
                        <textarea cols="60" rows="2" name="Customer[comment]" id="comment"><?php echo isset($customer['comment']) ? $customer['comment'] : ''; ?></textarea>
                    </div>
                    <div class="clear"></div>
                </li>
            </ul>
            <div style="text-align: right;">
                <input class="btn btn-success btn-large" type="submit" value="Check out">
            </div>
        </form>
        
    </div>

</div>
<script>
    function checkForm(){
        var check = true;
        $.each($("input.flowfVerify"), function(i, n){
            var str = $(n).val();
            var verify = $(n).attr("rel-verify");
            switch(verify){
                case 'required':
                    if(str==""){
                        $(n).next('font').remove();
                        $(n).after(" <font color='red'>"+$(n).attr("rel-message")+"</font>");
                        $(n).focus();
                        check = false;
                        return false;
                    }else{
                        $(n).next('font').remove();
                    }
                    break;
            }
            
        });
        return check;
    }
    //check coupon
    function checkCoupon(){
        var url = "<?php echo $this->createUrl('checkCoupon'); ?>";
        if($("#coupon").val()!=""){
            $.post(url, {coupon:$("#coupon").val()}, function(data){
                if(data == 0){
                    alert('Coupon error!');
                    $("#coupon").val("");
                }else{
                    alert("OK!");
                }
            });
        }
        
    }
    
    //product reduce
    function reduce(num, obj){
        var now_num = $(obj).parent().find('input').val();
        if(now_num>1){
            --now_num;
            $(obj).parent().find('input').val(now_num);
            $.post("<?php echo $this->createUrl('updateNum'); ?>", {order_key:$(obj).attr("rel-key"), num:now_num}, function(data){
                if(data){
                    $("#total-price").html(data);
                }
            });
        }
    }
    
    //product increase
    function increase(num, obj){
        var now_num = $(obj).parent().find('input').val();
        ++now_num;
        $(obj).parent().find('input').val(now_num);
        $.post("<?php echo $this->createUrl('updateNum'); ?>", {order_key:$(obj).attr("rel-key"), num:now_num}, function(data){
            $("#total-price").html(data);
        });
    }
</script>
